package logic

import (
	"context"
	"fmt"
	"github.com/jinzhu/gorm"
	"status/model/mysql"
	"status/statusrpc/internal/svc"
	"status/statusrpc/statusrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLogic) Add(in *statusrpc.AddRequest) (*statusrpc.AddResponse, error) {
	// todo: add your logic here and delete this line

	add := mysql.User{
		Model:   gorm.Model{},
		Store:   in.Store,
		Address: in.Address,
	}

	if in.Store == "" && in.Address == "" {
		return nil, fmt.Errorf("不能为空")
	}

	mysql.DB.Debug().Model(&add).Create(&add)

	return &statusrpc.AddResponse{Id: int64(add.ID)}, nil
}
